<?php


namespace app\common\model;


use think\facade\Db;
use think\Model;
class Member extends Model
{
    public function memberInfo()
    {
        return $this->hasOne(MemberInfo::Class,'member_id');
//        ->bind([
//        'm_id' =>'id',
//        'name',
//        'member_category',
//        'fund',
//        'contacts',
//        'position',
//        'mobile',
//        'landine_phone',
//        'member_grade',
//        'member_time',
//        'commend_flag',
//        'status'
//    ])
    }
    public function consignorReplenish(){
        return $this->hasOne(ConsignorReplenish::Class,'member_id');
    }
    public function logisticsReplenish(){
        return $this->hasOne(LogisticsReplenish::Class,'member_id');
    }
    public function applyMemberInfo()
    {
        return $this->hasMany(MemberInfoAddUser::class,'member_info_id');
    }
    public function addMember($data){
        $result = $this->save($data);
        return $result;
    }
    public function getMemberByWhere($where){
        $result = $this->where($where)->find();
        return $result;
    }
    public function updateMemberByWhere($where,$data){
        $result = $this->where($where)->find()->save($data);
        return $result;
    }
    public function getMemberLogisticsById($id){
        $result = $this->with(['memberInfo','logisticsReplenish'])->find($id);
        return $result;
    }
    public function getMemberInfoById($id){
        $result = $this->with(['memberInfo'])->find($id);
        return $result;
    }
    public function getMemberConsignorById($id){
        $result = $this->with(['memberInfo','consignorReplenish'])->find($id);
        return $result;
    }
    public function removeMember($id){
        $member = $this->find($id);
        $result = $member->delete();
        return $result;
    }

    public function removeMemberInfo($id){
        $member = $this->find($id);
        $memberInfo = $member->with('memberInfo')->find($id);
        $result = $memberInfo->together(['memberInfo'])->delete();
        return $result;
    }
    public function getListByCategory($phone,$paging,$where){
        $count = Db::table('member')
            ->alias('a')
            ->join(['member_info'=>'b'],'a.id=b.member_id')
            ->where('phone','like','%'.$phone.'%')
            ->count();
        $total = ceil($count/$paging['num']);
        $res = Db::table('member')
            ->alias('a')
            ->join(['member_info'=>'b'],'a.id=b.member_id')
            ->where('phone','like','%'.$phone.'%')
            ->where($where)
            ->order('a.id', 'desc')
            ->limit(($paging['current_page']-1)*$paging['num'],$paging['num'])
            ->select();
        $pageInfo = [
            "current_page"=>$paging['current_page'],
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }

    public function getListByCategoryTime($phone,$start_time,$end_time,$paging,$where){
        $count = Db::table('member')
            ->alias('a')
            ->join(['member_info'=>'b'],'a.id=b.member_id')
            ->where('phone','like','%'.$phone.'%')
            ->where($where)
            ->whereTime('create_time','between',[$start_time,$end_time])
            ->count();
        $total = ceil($count/$paging['num']);
        $res = Db::table('member')
            ->alias('a')
            ->join(['member_info'=>'b'],'a.id=b.member_id')
            ->where('phone','like','%'.$phone.'%')
            ->whereTime('create_time','between',[$start_time,$end_time])
            ->where($where)
            ->order('a.id', 'desc')
            ->limit(($paging['current_page']-1)*$paging['num'],$paging['num'])
            ->select();
        $pageInfo = [
            "current_page"=>$paging['current_page'],
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }

    /**分页获取member以及memberInfo信息
     */
    public function getList($phone,$paging){
        $count = $this->where('phone','like','%'.$phone.'%')
            ->count();
        $total = ceil($count/$paging['num']);
        $res =  $this->with('memberInfo')
            ->where('phone','like','%'.$phone.'%')
            ->order('id','desc')
            ->limit(($paging['current_page']-1)*$paging['num'],$paging['num'])
            ->select();

//        $res = \think\facade\Db::table('member')
//            ->alias('a')
//            ->join(['member_info'=>'b'],'a.id=b.member_id')
//            ->where('phone','like','%'.$phone.'%')
//            ->order('a.id', 'desc')
//            ->limit(($paging['current_page']-1)*$paging['num'],$paging['num'])
//            ->select();
        $pageInfo = [
            "current_page"=>$paging['current_page'],
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }

    public function getListByTime($phone,$start_time,$end_time,$paging){
//        dump($end_time);
//        dump($start_time);exit();
        $count = $this->where('phone','like','%'.$phone.'%')
            ->whereTime('create_time','between',[$start_time,$end_time])
            ->count();
        $total = ceil($count/$paging['num']);
        $res =  $this->with('memberInfo')
            ->where('phone','like','%'.$phone.'%')
            ->whereTime('create_time','between',[$start_time,$end_time])
            ->order('id','desc')
            ->limit(($paging['current_page']-1)*$paging['num'],$paging['num'])
            ->select();
        $pageInfo = [
            "current_page"=>$paging['current_page'],
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }

    /**
     * 分页获取member信息
     * @param int $current_page
     * @param int $num
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getListPage($current_page=1,$num=10){
        $count = $this->count();
        $total = ceil($count/$num);
        $res =  $this->limit(($current_page-1)*$num,$num)->select();
        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }
    public function addMemberAll($memberId,$memberInfo,$supplement){
        $member = $this->find($memberId);
//        dump($supplement);exit();
        $data = [
            "memberInfo"=>$memberInfo,
            "consignorReplenish"=>$supplement
        ];
        $result = $member->with(['memberInfo','consignorReplenish'])->save($data);
//        $result = $member->memberInfo()->save($memberInfo);
        return $result;
    }
    public function addMemberInfo($memberId,$memberInfo){
        $member = $this->find($memberId);
        $result = $member->memberInfo()->save($memberInfo);
        return $result;
    }
    public function updateMemberInfo($memberId,$memberInfo){
        $member = $this->find($memberId);
        $member->memberInfo = $memberInfo;
        $result = $member->together(['memberInfo'])->save();
        return $result;
    }
    public function addConsignorReplenish($memberId,$supplement){
        $member = $this->find($memberId);
        $result = $member->consignorReplenish()->save($supplement);
        return $result;
    }
    public function updateConsignorReplenish($memberId,$supplement){
        $member = $this->find($memberId);
        $member->consignorReplenish = $supplement;
        $result = $member->together(['consignorReplenish'])->save();
        return $result;
    }
    public function updateMemberAll($memberId,$memberInfo,$supplement){
        $member = $this->find($memberId);
        $member->memberInfo = $memberInfo;
        $member->consignorReplenish = $supplement;
        $result = $member->together(['memberInfo','consignorReplenish'])->save();
//        $result = $member->memberInfo()->save($memberInfo);
        return $result;
    }

    public function addLogisticsReplenish($memberId,$supplement){
        $member = $this->find($memberId);
        $result = $member->logisticsReplenish()->save($supplement);
        return $result;
    }
    public function updateLogisticsReplenish($memberId,$supplement){
        $member = $this->find($memberId);
        $member->logisticsReplenish = $supplement;
        $result = $member->together(['logisticsReplenish'])->save();
        return $result;
    }
    public function updateMemberLogisticsAll($memberId,$memberInfo,$supplement){
        $member = $this->find($memberId);
        $member->memberInfo = $memberInfo;
        $member->logisticsReplenish = $supplement;
        $result = $member->together(['memberInfo','logisticsReplenish'])->save();
//        $result = $member->memberInfo()->save($memberInfo);
        return $result;
    }
}